package com.shigen.demo.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shigen.demo.common.dto.PaginationDTO;
import com.shigen.demo.common.query.AggregateQueries;
import com.shigen.demo.common.query.UserFuzzyQueries;
import com.shigen.demo.common.query.UserQueries;
import com.shigen.demo.common.util.AggregateQueriesUtil;
import com.shigen.demo.dao.UserMapper;
import com.shigen.demo.entity.Result;
import com.shigen.demo.entity.User;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shigenfu
 * @date 2023/8/19 5:22 下午
 */
@RestController

public class CommonQueryController {

    @Resource
    private UserMapper userMapper;

    @PostMapping(value = "index/query")
    public Result<List<User>> get(@RequestBody AggregateQueries<UserQueries, UserFuzzyQueries> aggregate) {
        PaginationDTO pagination = aggregate.getPagination();
        QueryWrapper<User> wrapper = AggregateQueriesUtil.splicingAggregateQueries(new QueryWrapper<>(), aggregate);
        Page<User> page = new Page<>(pagination.getPageNum(), pagination.getPageSize());
        Page<User> userPage = userMapper.selectPage(page, wrapper);
        List<User> records = userPage.getRecords();
        return Result.ok(records);
    }


}
